package com.annvcit.imagewrite;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @author hqthai01 Jan 22, 2015
 */
public class MyImageWriter {
	private PropertyLoader propertyLoader;
	private String outputHomeFolder;

	public MyImageWriter() {
		this.propertyLoader = new PropertyLoader("/props/card.properties");
		this.outputHomeFolder = "/home/hqthai01/Desktop/";
	}

	public void setOutputHomeFolder(String outputHomeFolder) {
		this.outputHomeFolder = outputHomeFolder;
		new File(outputHomeFolder).mkdirs();
	}

	public String getOutputHomeFolder() {
		return this.outputHomeFolder;
	}

	public String createCard(CardInfo cardInfo) {
		String outputUrl = outputHomeFolder + cardInfo.getFullName() + "_" + getTimeStamp() + ".png";
		try {

			PositionAndFont jobP = getPosition(cardInfo.getCardName(), "job");
			PositionAndFont orgP = getPosition(cardInfo.getCardName(), "org");
			PositionAndFont fullNameP = getPosition(cardInfo.getCardName(), "fullname");
			PositionAndFont phoneP = getPosition(cardInfo.getCardName(), "phone");
			PositionAndFont faxP = getPosition(cardInfo.getCardName(), "fax");
			PositionAndFont emailP = getPosition(cardInfo.getCardName(), "email");
			PositionAndFont websiteP = getPosition(cardInfo.getCardName(), "website");
			PositionAndFont addressP = getPosition(cardInfo.getCardName(), "address");
			Rectangle imageRect = getImageRect(cardInfo.getCardName(), "image");

			BufferedImage img = ImageIO.read(MyImageWriter.class.getResource("/cards/" + cardInfo.getCardName() + ".png"));

			Image image = new ImageIcon(cardInfo.getAvatarPath()).getImage();
			Graphics g = img.getGraphics();
			g.setFont(fullNameP.getFont());
			g.setColor(fullNameP.getColor());
			g.drawString(cardInfo.getFullName(), fullNameP.getX(), fullNameP.getY());
			
			g.setFont(jobP.getFont());
			g.setColor(jobP.getColor());
			g.drawString("Job: " + cardInfo.getJob(), jobP.getX(), jobP.getY());
			
			g.setFont(orgP.getFont());
			g.setColor(orgP.getColor());
			g.drawString(cardInfo.getOrganization(), orgP.getX(), orgP.getY());
			
			g.setFont(phoneP.getFont());
			g.setColor(phoneP.getColor());
			g.drawString("Phone: " + cardInfo.getPhone(), phoneP.getX(), phoneP.getY());
			
			g.setFont(faxP.getFont());
			g.setColor(faxP.getColor());
			g.drawString("Fax: " + cardInfo.getFax(), faxP.getX(), faxP.getY());
			
			g.setFont(emailP.getFont());
			g.setColor(emailP.getColor());
			g.drawString("Email: " + cardInfo.getEmail(), emailP.getX(), emailP.getY());
			
			g.setFont(websiteP.getFont());
			g.setColor(websiteP.getColor());
			g.drawString("Website: " + cardInfo.getWebsite(), websiteP.getX(), websiteP.getY());
			
			g.setFont(addressP.getFont());
			g.setColor(addressP.getColor());
			g.drawString("Address: " + cardInfo.getAddress(), addressP.getX(), addressP.getY());

			g.drawImage(image, (int) imageRect.getX(), (int) imageRect.getY(), (int) imageRect.getWidth(), (int) imageRect.getHeight(), null);
			g.dispose();
			File file = new File(outputUrl);
			outputUrl = file.getAbsolutePath();
			ImageIO.write(img, "png", file);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return outputUrl;
	}

	private PositionAndFont getPosition(String... values) {
		String keyX = values[0] + "_" + values[1] + "_x";
		String keyY = values[0] + "_" + values[1] + "_y";
		String key_fontName = values[0] + "_font_name";
		String key_fontSize = values[0] + "_" + values[1] + "_font_size";
		String key_fontStyle = values[0] + "_" + values[1] + "_font_style";
		String key_color = values[0] + "_" + values[1] + "_color";

		int x = Integer.parseInt(propertyLoader.getProperty(keyX));
		int y = Integer.parseInt(propertyLoader.getProperty(keyY));
		String fontName = propertyLoader.getProperty(key_fontName);
		int fontSize = Integer.parseInt(propertyLoader.getProperty(key_fontSize));
		int fontStyle = Integer.parseInt(propertyLoader.getProperty(key_fontStyle));
		Font font = new Font(fontName, fontStyle, fontSize);

		Color color = getColor(propertyLoader.getProperty(key_color));
		return new PositionAndFont(x, y, font, color);
	}

	private Color getColor(String colorName) {
		switch(colorName){
		case "black":
			return Color.black;
		case "blue" :
			return Color.blue;
		case "red":
			return Color.red;
		case "white":
			return Color.white;
		}
		return null;
	}

	private Rectangle getImageRect(String... values) {
		String keyX = values[0] + "_" + values[1] + "_x";
		String keyY = values[0] + "_" + values[1] + "_y";
		String keyW = values[0] + "_" + values[1] + "_w";
		String keyH = values[0] + "_" + values[1] + "_h";

		int x = Integer.parseInt(propertyLoader.getProperty(keyX));
		int y = Integer.parseInt(propertyLoader.getProperty(keyY));
		int w = Integer.parseInt(propertyLoader.getProperty(keyW));
		int h = Integer.parseInt(propertyLoader.getProperty(keyH));

		return new Rectangle(x, y, w, h);
	}

	private String getTimeStamp() {
		return new Date().toString().replaceAll(":", "-");
	}

	public static void main(String args[]) {
		MyImageWriter writer = new MyImageWriter();
		CardInfo cardInfo = new CardInfo();
		cardInfo.setAddress("An Giang");
		cardInfo.setAvatarPath("/home/hqthai01/Pictures/linh tinh/keys-icon.png");
		cardInfo.setCardName("card8");
		cardInfo.setEmail("hqthai01@hotmail.com");
		cardInfo.setFacebook("https://facebook.com/thai.huynhquang.5");
		cardInfo.setFax("1234-567-890");
		cardInfo.setFullName("Huynh Quang Thai");
		cardInfo.setJob("Software Architect");
		cardInfo.setModelName("m01_casualwear_00_h");
		cardInfo.setOrganization("Nong Lam University");
		cardInfo.setPhone("01636755316");
		cardInfo.setWebsite("https://hcmuaf.edu.vn");
		System.out.println(cardInfo);

		System.out.println(writer.createCard(cardInfo));
	}
}